from HTMLParser import HTMLParser
import httplib
from parse import *
import time                  
from datetime import datetime
from gmail import GMail,GMailWorker,GMailHandler
from message import Message
import smtplib
from htmlentitydefs import name2codepoint
#from telegram import telegram_send
latestItemNum = 0

#def send_telegram(msg):
	#telegram_send(msg)

def send_email():

            import smtplib

            gmail_user = "kanghk.us@gmail.com"
            gmail_pwd = "gmlrnjs9"
            FROM = 'user@gmail.com'
            TO = ['kanghk21@gmail.com'] #must be a list
            SUBJECT = "Testing sending using gmail"
            TEXT = "Testing sending mail using gmail servers"
    
            # Prepare actual message
            message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
            """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
            try:
                #server = smtplib.SMTP(SERVER) 
                server = smtplib.SMTP("smtp.gmail.com", 587) #or port 465 doesn't seem to work!
                server.ehlo()
                server.starttls()
                server.login(gmail_user, gmail_pwd)
                server.sendmail(FROM, TO, message)
                #server.quit()
                server.close()
                print 'successfully sent the mail'
            except:
                print "failed to send mail"
def send_email2():
      import smtplib

      server = smtplib.SMTP_SSL('smtp.daum.net', 465)
      server.set_debuglevel(1)
      server.ehlo()
      server.login('xeon2', 'gmlrins113')
      server.sendmail('kanghk21@gmail.com', 'hi\r\n')
      server.close()

class MyHTMLParser(HTMLParser):
    def __init__(self):
        HTMLParser.__init__(self)
        self.recording = 0
        self.data=''
        self.self_closing_tags = ("br",)
        self.checkTitle = 0
        self.inLITag = 0
        self.foundAmazon = 0

    def handle_data(self, data):
        if self.checkTitle > 0:
            if data.lower().find("amazon") != -1:
                print data
                self.foundAmazon = 1
                print "Found Item~~~~~~~~~~~~\n\n"
                time.sleep(1)
                print "\tFound Item~~~~~~~~~~~~\n\n"
                time.sleep(1)
                print "\t\tFound Item~~~~~~~~~~~~\n\n"
                time.sleep(1)
                print "\t\t\tFound Item~~~~~~~~~~~~\n\n"
                time.sleep(1)
                print "\t\t\t\tFound Item~~~~~~~~~~~~\n\n"

                # TODO : Create Notifications
              

    def handle_endtag(self, tag):
        #print "End tag  :", tag
        if tag in ('li',):
            self.inLITag -= 1; #decrease reference count
            if self.inLITag == 0:
                self.checkTitle = 0
                #print "END:",self.checkTitle

    def handle_starttag(self, tag, attrs):
        #print "Start tag:", tag
        #if self.checkTitle == 1 :
        if tag in ('li',):
            self.inLITag += 1  #increase reference count
        if tag in ('a',):
            if self.inLITag is 1:
                for attr in attrs:
                    #print attr[1]
                    global latestItemNum
                    #print "lastestItemNum:",latestItemNum
                    result = search('bbs_view.php?id=ppomppu4&no={:d}',attr[1])
                    if result is not None  and latestItemNum < result[0]:
                        latestItemNum = result[0]
                        self.checkTitle = 1 
                        #print "SET:",self.checkTitle

                           

print "Start Parser"
while(1):
    print "loop"
    parser = MyHTMLParser()
    
    httpHandle = httplib.HTTPConnection('m.ppomppu.co.kr')
    httpHandle.request("GET", "/new/bbs_list.php?id=ppomppu4")
    r1 = httpHandle.getresponse()
    data1 = r1.read()

    #print data1
    #print "######"

    parser.feed(data1)
    del parser
    time.sleep(5)

